require(['init'], function () {
    var ue=UE.getEditor('editor');
    $('#submit_html').val(i18next.t('course.submit'))
    //上传图片
    var $imgUpload=$("#J_imgupload")
    function imgUpload($container) {
        var $uploadCoverBox = $container.find('.icon-uploadimg')
        var $uploadCoverThumb = $uploadCoverBox.find('img')
        var $uploadInput = $uploadCoverBox.find('input')
        var coverUploadInput = new UploadInput({
            $dom: $uploadInput,
            done: function (uploadImgSrc) {
                var uploadImgFullSrc='/api/file?u=' + uploadImgSrc;
                // if (uploadImgSrc) {
                //     $uploadCoverThumb.attr('src', uploadImgFullSrc);
                //     $uploadCoverBox.attr('data-value', uploadImgFullSrc);
                //     return;
                //   }
                if (uploadImgSrc) {
                  new ImgCropTool({
                    imgSrc: uploadImgFullSrc,
                    cropSize:[480, 270, 'courseCover'],
                    success:function(imgSrc){
                      var imgCropSrc = '/api/file?u=' + imgSrc;
                      $uploadCoverThumb.attr('src', imgCropSrc);
                      $uploadCoverBox.attr('data-value', imgCropSrc);
                    //  return;
                    }
                  })
                }
            }
        });
    }
    imgUpload($imgUpload);
   //点击编辑传值
    function loadEnrollData(id, callback) {
        ajaxMethod({
            url:'/api/marketing/post/'+id,
            success: function (data) {
                $('input[name="title"]').val(data.title);
                $('input[name="id"]').val(data.id);
                $('input[name="resourse"]').val(data.resourse);
                $('input[name="description"]').val(data.description);
                $("#J_img").attr('src', data.cover);
                //获取附文本编辑器的内容
                ue.ready(function(){
                  ue.setContent(data.body);
                });
                (typeof callback == 'function') && callback();
            }
        })
    }
  //点击添加
  var sta="";
  function bindSubmit(){
    $("#submit_html").on('click',function(){
      if(FormCheck.childrenInputCheck($('#marketing_agencyform'))){
        var checked = $('[type="checkbox"]').hasClass('checked')
        var id=$('input[name="id"]').val();
        var title=$('input[name="title"]').val();
        var resourse=$('input[name="resourse"]').val();
        var cover = $('#J_uploadCover img').attr('src');
        var description=$('input[name="description"]').val();
        //附文本编辑器的内容
        var UeditorTEXT=ue.getContent();
        var arr={
          id:id,
          title:title,
          cover:cover,
          body:UeditorTEXT,
          resourse:resourse,
          description:description
        }
        if (editFlag) {
          ajaxMethod({
              url: '/api/marketing/post/'+id,
              type: 'put',
              data: arr,
              success: function (data) {
                  // alert("编辑")
                  location.replace("marketing_agencylist.html");

              }
          });

      } else {
          ajaxMethod({
              url: '/api/marketing/post',
              type: 'post',
              data: arr,
              success: function (data) {
                // alert("保存");
                location.replace("marketing_agencylist.html");
              }
          });
        }
      }
    })
  }

  var id = location.search.urlToObj().id
      var editFlag = false
      if (id) {
          editFlag = true
          loadEnrollData(id, bindSubmit)
          return
      }
      bindSubmit()
})
